Skip to content

Conversation

KTibow
Copy link

@KTibow KTibow commented Jun 30, 2025

If you have a typed subarray for whatever reason, like using pbf's readBytes, devalue currently stringifies the whole buffer as shown in this example:

import { parse, stringify } from "npm:devalue";

const buffer = new ArrayBuffer(100);
const view = new Uint8Array(buffer);
const subarray = view.subarray(0, 10);

console.log(subarray.length); // 10
console.log(parse(stringify(subarray)).length); // Incorrectly 100

This fixes that by calling .slice() first.

@Rich-Harris
Copy link
Member

Thank you. I don't think this is quite right though — while it's definitely true that the deserialized array should have the right bounds, it wouldn't be correct, strictly speaking, to slice the array:

const buffer = new ArrayBuffer(100);
const view = new Uint8Array(buffer);
const subarray = view.subarray(0, 10);

const reconstructed = new Uint8Array(subarray.buffer);

In many cases you probably do want the buffer to be sliced, but that's something that should really happen prior to serialization. While looking into this I realised we're also failing to handle the case where an array buffer backs multiple views, so I opened #105 — will close this in favour of that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants